package com.wanpinghui.supplier.object.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.wphlibrary.model.object.bean.FeedBackReasonBean;

import java.util.List;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/12/29
 ***/

public class FeedBackConfirmAdapter extends BaseExpandableListAdapter {
    private List<FeedBackReasonBean> datas;
    private Context context;
    private OnReasonClickListener onReasonClickListener;

    public FeedBackConfirmAdapter(Context context) {
        this.context = context;
    }

    public void setDatas(List<FeedBackReasonBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void setOnReasonClickListener(OnReasonClickListener listener) {
        onReasonClickListener = listener;
    }

    @Override
    public int getGroupCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return datas.get(groupPosition).getDatas() == null ? 0 : datas.get(groupPosition).getDatas().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recv_feedback_reason_p, null);
        LinearLayout layout = (LinearLayout) view.findViewById(R.id.ll_area);
        ImageView imageView = (ImageView) view.findViewById(R.id.iv_select);
        TextView textView = (TextView) view.findViewById(R.id.tv_name);
        if (getChildrenCount(groupPosition) == 0) {
            imageView.setVisibility(View.GONE);
        } else {
            imageView.setVisibility(View.VISIBLE);
        }
        if (isExpanded) {
            imageView.setImageResource(R.mipmap.my_icon_arrow_up);
        } else {
            imageView.setImageResource(R.mipmap.my_icon_);
        }
        if (datas.get(groupPosition).isEnable()) {
            SpannableString spannableString = new SpannableString(datas.get(groupPosition).getName());
            spannableString.setSpan(new StyleSpan(Typeface.BOLD), 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            textView.setText(spannableString);
            textView.setTextColor(ContextCompat.getColor(context, R.color.colorTextPrimaryOne));
        } else {
            SpannableString spannableString = new SpannableString(datas.get(groupPosition).getName());
            spannableString.setSpan(new StyleSpan(Typeface.NORMAL), 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            textView.setText(spannableString);
            textView.setTextColor(ContextCompat.getColor(context, R.color.colorTextPrimaryTwo));
        }
        if (getChildrenCount(groupPosition) == 0) {
            layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onReasonClickListener != null) {
                        onReasonClickListener.onGroupClick(groupPosition, datas.get(groupPosition).getKey());
                    }
                }
            });
        }


        return view;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recv_feedback_reason_p, null);
        LinearLayout layout = (LinearLayout) view.findViewById(R.id.ll_area);
        layout.setBackgroundColor(ContextCompat.getColor(context, R.color.colorBackgroundF7));
        TextView textView = (TextView) view.findViewById(R.id.tv_name);
        ImageView imageView = (ImageView) view.findViewById(R.id.iv_select);
        textView.setTextColor(ContextCompat.getColor(context, R.color.colorTextPrimaryTwo));
        textView.setText(datas.get(groupPosition).getDatas().get(childPosition).value);
        imageView.setVisibility(View.GONE);
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onReasonClickListener != null && datas.get(groupPosition).getDatas().get(childPosition).key != 0) {
                    onReasonClickListener.onChlidClick(groupPosition, childPosition, datas.get(groupPosition).getDatas().get(childPosition).key);
                }
            }
        });
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }

    public interface OnReasonClickListener {
        void onGroupClick(int position, int key);

        void onChlidClick(int groupPostion, int childPosition, int key);
    }
}
